//
//  MyDataService.m
//  NewSinaweibo
//
//  Created by lilubin on 15/4/17.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "MyDataService.h"
#import "AFNetworking.h"

#define BASE_URL @"http://cunli.zhanyaa.com/yifaapi/app"
//#define BASE_URL @"http://121.41.117.249/yifaapi/app"

@implementation MyDataService

+(AFHTTPRequestOperation *)requestURL:(NSString *)urlstring httpMethod:(NSString *)method params:(NSDictionary *)params fileDatas:(NSDictionary *)fileDatas complection:(void (^)(id))block failureAction:(void (^)(void))errorBlock
{
    //拼接url
    NSString *url = [BASE_URL stringByAppendingPathComponent:urlstring];
    
    NSLog(@"url:%@", url);
    
    NSLog(@"IN DATASERVICE:%@", params);
    
    //设置字典
    NSMutableDictionary *mutableParams = [params mutableCopy];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *operation = nil;
    
    if ([method isEqualToString:@"GET"])
    {
        operation = [manager GET:url parameters:mutableParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error reason:%@", operation.responseString);
            NSLog(@"AFNetworkingHTTPRequest error:%@", error);
            errorBlock();
        }];
    }
    else if([method isEqualToString:@"POST"])
    {
        if (fileDatas == nil)
        {
            operation = [manager POST:url parameters:mutableParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
                block(responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"In dataService:operation.responseString:%@", operation.responseString);
                NSLog(@"In dataService:AFNetworking error:%@", error);
                errorBlock();
            }];
        }
        else
        {
            
            
            manager.requestSerializer=[AFHTTPRequestSerializer serializer];
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            operation = [manager POST:url parameters:mutableParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                for(id key in fileDatas)
                {
                    NSArray *arr=fileDatas[key];
                    
                    for (int i=0; i<arr.count; i++) {
                        
                        NSData *data=[arr objectAtIndex:i];
                        [formData appendPartWithFileData:data name:key fileName:key mimeType:@"image/jpeg"];
                    }
                    
                }
            } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                
//                NSLog(@"Request Successfully");
                block(responseObject);
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"网络请求失败:%@",error);
                NSLog(@"operation:%@", operation.responseString);

                errorBlock();
            }];
        }

    }
    return operation;
}

@end
